package org.yellow.backend.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.yellow.backend.mapper.doctorMapper;
import org.yellow.backend.pojo.doctor;

import java.util.List;

/**
 * @author: hy
 * @create: 2024-01-01
 */
@Service
public class doctorService {
    @Autowired
    doctorMapper docMap;

    /**
     * 通过ID查找医生
     * @param  id
     * @return doctor
     */
    public doctor selectDocById(int id){
        return docMap.selectById(id);
    }

    /**
     * 根据username修改医生其他信息
     * @param doc
     * @return
     */
    public boolean updateDoc(doctor doc){
        LambdaUpdateWrapper<doctor> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(doctor::getUserName, doc.getUserName())
                .set(doctor::getPassword, doc.getPassword())
                .set(doctor::getName, doc.getName())
                .set(doctor::getSpecialization,doc.getSpecialization())
                .set(doctor::getContactNumber,doc.getContactNumber());
        docMap.update(null, lambdaUpdateWrapper);
        return true;
    }

    /**
     * 查找相应科室下所有医生
     * @param depId
     * @return
     */
    public List<doctor> selectDocByDep(int depId){
        LambdaQueryWrapper<doctor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(doctor::getDepartmentId,depId);

        return docMap.selectList(wrapper);
    }

    /**
     * 根据医生id查找可预约时间段
     * @param docId
     * @return
     */
    public String selectAppTime(int docId){
        QueryWrapper<doctor> w = new QueryWrapper<>();
        w.select("appointment_time").eq("doctor_id",docId);
        List<doctor> doctors = docMap.selectList(w);

        return doctors.get(0).getAppointmentTime();
    }

    /**
     * 查询所有医生信息
     * @return
     */
    public List<doctor> selectAllDoc(){
        return docMap.selectList(null);
    }
}
